package com.woniu.gf.pawn.productentityweb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.gf.pawn.comm.core.dto.ResultDto;
import com.woniu.gf.pawn.comm.core.util.JwtUtil;
import com.woniu.gf.pawn.dto.AssessDto;
import com.woniu.gf.pawn.dto.IdentifyDto;
import com.woniu.gf.pawn.dto.PageAssAndIfdProductDto;
import com.woniu.gf.pawn.dto.PageDto;
import com.woniu.gf.pawn.param.AssessParam;
import com.woniu.gf.pawn.param.IdentifyParam;
import com.woniu.gf.pawn.param.PageParam;
import com.woniu.gf.pawn.productentityweb.service.IProductEntityAssessService;
import com.woniu.gf.pawn.productentityweb.service.IProductEntityIdentifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TL
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/product-entity-assess")
@Slf4j
@Api(tags = "商品评估")
public class ProductEntityAssessController {
    @Autowired
    private IProductEntityAssessService assessService;

    /**
     * 新增商品评估记录
     * @param request
     * @param assessDto
     * @return
     */
    @PostMapping("assessProduct")
    @Transactional
    @ApiOperation("新增商品评估记录")
    public ResultDto addIdentifyProduct(HttpServletRequest request, @RequestBody @Valid AssessDto assessDto){
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer managementId = (Integer) map.get("managementId");
        //暂时自定
        log.info("参数-{}",assessDto);
//        Integer managementId =3;
        log.info("参数-managementId-{}",managementId);
        AssessParam assessParam = new AssessParam();
        BeanUtils.copyProperties(assessDto,assessParam);
        assessParam.setAssessManId(managementId);
        assessService.addIdentifyProduct(assessParam);
        return ResultDto.success(200,"新增成功");
    }

    /**
     * 查询历史评估记录
     * @param pageAssAndIfdProductDto
     * @return
     */
    @GetMapping("findAssessProduct")
    @ApiOperation("查询历史评估记录")
    public ResultDto findAssessProduct(PageAssAndIfdProductDto pageAssAndIfdProductDto){
        log.info("参数-{}",pageAssAndIfdProductDto);

        if (pageAssAndIfdProductDto.getPageIdx() == null) {
            pageAssAndIfdProductDto.setPageIdx(1);
        }
        if (pageAssAndIfdProductDto.getPageSize() == null) {
            pageAssAndIfdProductDto.setPageSize(5);
        }
        PageParam pageParam = new PageParam();
        BeanUtils.copyProperties(pageAssAndIfdProductDto,pageParam);

        IPage<AssessDto> productBigTypeIPage = assessService.findAssessProduct(pageParam);

        return ResultDto.success(200,"查询成功",productBigTypeIPage);
    }
}

